package com.zju.DoctorGo.controller;

import com.zju.DoctorGo.dto.DoctorDTO;
import com.zju.DoctorGo.entity.Doctor;
import com.zju.DoctorGo.entity.Patient;
import com.zju.DoctorGo.entity.Result;
import com.zju.DoctorGo.mapper.DoctorMapper;
import com.zju.DoctorGo.mapper.PatientMapper;
import com.zju.DoctorGo.mapper.UserMapper;
import com.zju.DoctorGo.service.DoctorService;
import com.zju.DoctorGo.service.DoctorYellowPageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/doctor")
@Slf4j
public class DoctorController {
    @Autowired
    private DoctorYellowPageService doctorService;

    @Autowired
    private DoctorService doctorService1;

    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private PatientMapper patientMapper;

    @Autowired
    private UserMapper userMapper;

    @PutMapping("/updateDoctor")
    public void updateDoctor(@RequestBody Doctor doctor) {
        doctorMapper.update(doctor);
    }

    @GetMapping("/getAllDoctorsId")
    public List<Integer> getAllDoctorsId(){
        List<Doctor> doctors = doctorMapper.selectList(null);
        List<Integer> ids = new ArrayList<Integer>();
        for (Doctor doctor : doctors) {
            ids.add(doctor.getUserId());
        }
        return ids;
    }

    @GetMapping("/getAll")
    public Result getAllDoctor() {
        List<Doctor> doctors = doctorMapper.getAllDoctors();
        if (doctors != null && !doctors.isEmpty()) {
            return Result.success(doctors);
        }
        return Result.error("没有找到任何医生");
    }

    @GetMapping("/name/{doctorName}")
    public Result getDoctorByName(@PathVariable("doctorName") String doctorName) {
        System.out.println(doctorName);
        if (doctorName == null || doctorName.trim().isEmpty()) {
            return Result.error("请输入医生姓名");
        }
        Integer doctorId = doctorService.getByName(doctorName);
        if (doctorId != null && doctorId != 0) {
            return Result.success(doctorId);
        }
        return Result.error("找不到该名字的医生");
    }

    @GetMapping("/id/{doctorId}")
    public Result getDoctorById(@PathVariable("doctorId") Integer doctorId) {
        if (doctorId == null || doctorId == 0) {
            return Result.error("请输入医生ID");
        }
        Doctor doctor = doctorService.retrieveDoctorById(doctorId);
        if (doctor != null) {
            return Result.success(doctor);
        }
        return Result.error("找不到该ID的医生");
    }

    @GetMapping("/specialty/{specialty}")
    public Result getDoctorBySpecialty(@PathVariable("specialty") String specialty) {
        if (specialty == null || specialty.trim().isEmpty()) {
            return Result.error("请输入科室名称");
        }
        List<DoctorDTO> doctors = doctorService.retrieveDoctorsBySpecialty(specialty);
        if (doctors != null && !doctors.isEmpty()) {
            return Result.success(doctors);
        }
        return Result.error("找不到该专业的医生");
    }

    @GetMapping("/patientManage")
    public Result getPatientManager(@RequestParam Integer doctorId) {
        List<Patient> patients = doctorService1.getPatientsByDoctorId(doctorId);
        return Result.success(patients);
    }

}
